﻿using EasyDDD.AdminModule.Application.CommandsSysAdminRole;
using EasyDDD.AdminModule.Application.QueriesSysAdminRole;
using Microsoft.AspNetCore.Http;

namespace EasyDDD.AdminModule.Host.WebAPI.Controllers
{
    /// <summary>
    /// 系统角色管理
    /// </summary>
    /// <param name="_mediator"></param>
    /// <param name="_mapper"></param>
    [Route("api/AdminModule/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class SysAdminRoleController(IMediator _mediator, IMapper _mapper) : ControllerBase
    {
        /// <summary>
        /// 获取角色列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpGet]
        [EndpointSummary("获取角色列表")]
        [EndpointDescription("AdminModule.SysAdminRole.GetPageList")]
        [AuthorizeClaim(ClaimTypes.MenuPermissions, "AdminModule.SysAdminRole.GetPageList")]
        public async Task<List<Dto.SysAdminRoleGetListResult>> GetList([FromQuery] SysAdminRoleGetListQuery query)
        {
            var list = await _mediator.Send(query, HttpContext.RequestAborted);
            return _mapper.Map<List<Dto.SysAdminRoleGetListResult>>(list);
        }

        /// <summary>
        /// 获取角色菜单ID
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        [EndpointSummary("获取角色菜单ID")]
        public Task<Guid[]> GetMenuIds([FromQuery] SysAdminRoleGetMenuIdsQuery command)
        {
            return _mediator.Send(command, HttpContext.RequestAborted);
        }

        /// <summary>
        /// 获取分页列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpGet]
        [EndpointSummary("获取分页列表")]
        [EndpointDescription("AdminModule.SysAdminRole.GetPageList")]
        [AuthorizeClaim(ClaimTypes.MenuPermissions, "AdminModule.SysAdminRole.GetPageList")]
        public async Task<Dto.SysAdminRoleGetPageListResult> GetPageList([FromQuery] SysAdminRoleGetPageListQuery query)
        {
            var list = await _mediator.Send(query, HttpContext.RequestAborted);
            return _mapper.Map<Dto.SysAdminRoleGetPageListResult>(list);
        }

        /// <summary>
        /// 添加角色
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        [EndpointSummary("添加角色")]
        [EndpointDescription("AdminModule.SysAdminRole.Add")]
        [AuthorizeClaim(ClaimTypes.MenuPermissions, "AdminModule.SysAdminRole.Add")]
        public async Task<Dto.SysAdminRoleCreateResult> Create([FromBody] SysAdminRoleCreateCommand command)
        {
            var entity = await _mediator.Send(command, HttpContext.RequestAborted);
            return _mapper.Map<Dto.SysAdminRoleCreateResult>(entity);
        }

        /// <summary>
        /// 更新角色
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPut]
        [EndpointSummary("更新角色")]
        [EndpointDescription("AdminModule.SysAdminRole.Update")]
        [AuthorizeClaim(ClaimTypes.MenuPermissions, "AdminModule.SysAdminRole.Update")]
        public async Task<Dto.SysAdminRoleUpdateResult> Update([FromBody] SysAdminRoleUpdateCommand command)
        {
            var entity = await _mediator.Send(command, HttpContext.RequestAborted);
            return _mapper.Map<Dto.SysAdminRoleUpdateResult>(entity);
        }

        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpDelete]
        [EndpointSummary("更新角色")]
        [EndpointDescription("AdminModule.SysAdminRole.Delete")]
        [AuthorizeClaim(ClaimTypes.MenuPermissions, "AdminModule.SysAdminRole.Delete")]
        public Task Delete([FromBody] SysAdminRoleDeleteCommand command)
        {
            return _mediator.Send(command, HttpContext.RequestAborted);
        }

        /// <summary>
        /// 设置角色状态
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPut]
        [EndpointSummary("设置角色状态")]
        [EndpointDescription("AdminModule.SysAdminRole.SetStatus")]
        [AuthorizeClaim(ClaimTypes.MenuPermissions, "AdminModule.SysAdminRole.SetStatus")]
        public async Task<Dto.SysAdminRoleSetStatusResult> SetStatus([FromBody] SysAdminRoleSetStatusCommand command)
        {
            var entity = await _mediator.Send(command, HttpContext.RequestAborted);
            return _mapper.Map<Dto.SysAdminRoleSetStatusResult>(entity);
        }

        /// <summary>
        /// 设置角色菜单
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPut]
        [HttpPut]
        [EndpointSummary("设置角色菜单")]
        [EndpointDescription("AdminModule.SysAdminRole.SetMenu")]
        [AuthorizeClaim(ClaimTypes.MenuPermissions, "AdminModule.SysAdminRole.SetMenu")]
        public Task SetMenu([FromBody] SysAdminRoleSetMenuCommand command)
        {
            return _mediator.Send(command, HttpContext.RequestAborted);
        }
    }
}
